﻿using System;
using Demo.CommonBase.View.Forms;
using Demo.Services.Contracts.DTO.Infos;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Ext.Net;
using Demo.Services.Services.Impl.Managers;

namespace Demo.Web.Catalogs
{
    public partial class TecnologyEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id;                
                if (Request["Id"] != null)
                {
                    id = Convert.ToInt32(Request["Id"]);
                    SetEditData(id);
                }
            }
        }
        private void SetEditData(int id)
        {
            TecnologyManager tecnologyManager;
            TecnologyInfo tecnologyInfo;

            try
            {
                Logger.Info();
                tecnologyManager = new TecnologyManager();

                tecnologyInfo = tecnologyManager.GetTecnologyById(id);
                txtTecnologyID.Text = tecnologyInfo.Id.ToString();
                txtName.Text = tecnologyInfo.Description;                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
        }        

        protected void Guardar(object sender, DirectEventArgs e)
        {
            TecnologyManager tecnologyManager;
            TecnologyInfo tecnologyInfo;
            try
            {
                tecnologyManager = new TecnologyManager();
                tecnologyInfo  = GetData();
                tecnologyManager.SaveTecnology(tecnologyInfo);

                ExtNet.Js.AddScript("parentAutoLoadControl.hide();");
                ExtNet.Js.AddScript("parent.Ext.getCmp('btnRefresh').fireEvent('click');");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }

        }
        private TecnologyInfo GetData()
        {
            var tecnologyInfo = new TecnologyInfo
                                    {
                                        Id = txtTecnologyID.Text == string.Empty ? 0 : Convert.ToInt32(txtTecnologyID.Text),
                                        Description = txtName.Text
                                    };
            return tecnologyInfo;
        }
        protected void CheckName(object sender, RemoteValidationEventArgs e)
        {
            int id = txtTecnologyID.Text == string.Empty ? 0 : Convert.ToInt32(txtTecnologyID.Text);
            var name = (TextField)sender;
            var tecnologyManager = new TecnologyManager();
            bool isValid = tecnologyManager.CheckName(id, name.Text);


            if (isValid)
            {
                e.Success = true;
            }
            else
            {
                e.Success = false;
                e.ErrorMessage = "Nombre de Tecnología Existente";
            }

            System.Threading.Thread.Sleep(1000);
        }
    }
}